var menuManager_page = {};

menuManager_page.action = {
    initBody: function () {
        menuManager_page.interface.initTablePage();
        menuManager_page.action.bindEvent();
    },
    bindEvent: function () {
    },
    deleteMenu: function (id,level) {
        AppComm.ajax.post("/menuManager/ajax/deleteMenu", {
            id: id,
            level:level
        }, function (resultData) {
            if (resultData.code==Enums.ResultEnum().SUCCESS.code) {
                AppComm.notice.success('删除成功！');
                menuManager_page.interface.initTablePage();
            }
        })
    }
};
menuManager_page.interface = {
    // 加载表格页
    initTablePage: function () {
        menuManager_page.data.permissionEdit = false;
        if (AppComm.permission.hasPermission("menuManager:edit")) {
            menuManager_page.data.permissionEdit = true;
        }
        menuManager_page.data.permissionDelete = false;
        if (AppComm.permission.hasPermission("menuManager:delete")) {
            menuManager_page.data.permissionDelete = true;
        }

        var html = template("menuManagerList_template");
        $(".menuManager_box").html(html);

        layui.config({
            base: '/static/comm/plugins/layui/lay/modules/',
        });
        layui.use(['treeTable','layer','code','form'],function(){

            menuManager_page.data.permissionEdit = false;
            if (AppComm.permission.hasPermission("menuManager:edit")) {
                menuManager_page.data.permissionEdit = true;
            }
            menuManager_page.data.permissionDelete = false;
            if (AppComm.permission.hasPermission("menuManager:delete")) {
                menuManager_page.data.permissionDelete = true;
            }

            var o = layui.$,
                form = layui.form,
                layer = layui.layer,
                treeTable = layui.treeTable;
            // 直接下载后url: './data/table-tree.json',这个配置可能看不到数据，改为data:[],获取自己的实际链接返回json数组


            AppComm.ajax.post('/menuManager/ajax/getMenuListByMenuKey', {"menuKey":""}, function (resultData) {
                var	re = treeTable.render({
                    elem: '#tree-table',
                    // url: AppComm.url.getBaseURL() + '/menuManager/ajax/getMenuTreeTable',
                    // data:[{"id":"bdf688db90d541399def53c574f97134","parentId":"0","level":1,"target":"_self","comment":"","href":"","orderId":1.0,"menuKey":"manager","menuName":"系统配置"},{"id":"f400c04e7517489cbee1ad1bee83947f","parentId":"bdf688db90d541399def53c574f97134","level":2,"target":"_self","comment":"","href":"manager/menuManager","orderId":1.0,"menuKey":"manager_menu","menuName":"菜单配置"},{"id":"9e95e5431e1a4c7c90ebfdcd5d77bb68","parentId":"bdf688db90d541399def53c574f97134","level":2,"target":"_self","comment":"","href":"manager/userManager","orderId":2.0,"menuKey":"manager_userManager","menuName":"用户配置"},{"id":"02ab5766bd064ea6801e448e8d214236","parentId":"bdf688db90d541399def53c574f97134","level":2,"target":"_self","comment":"","href":"manager/roleManager","orderId":3.0,"menuKey":"manager_roleManager","menuName":"角色配置"},{"id":"d88ed07605174ecb940f064fd8131cc2","parentId":"bdf688db90d541399def53c574f97134","level":2,"target":"_self","comment":"","href":"manager/operationManager","orderId":4.0,"menuKey":"manager_operationManager","menuName":"操作配置"},{"id":"7c1061bcd1414d29b5c5f4db42a7b42b","parentId":"bdf688db90d541399def53c574f97134","level":2,"target":"_self","comment":"","href":"manager/groupManager","orderId":5.0,"menuKey":"manager_groupManager","menuName":"组织管理"},{"id":"f114ffa019804a0685950431f368b6a8","parentId":"bdf688db90d541399def53c574f97134","level":2,"target":"_self","comment":"","href":"manager/modifyPassword","orderId":6.0,"menuKey":"manager_modifyPassword","menuName":"修改密码"}],
                    // data: [{"id":1,"parentId":0,"menuName":"1-1"},{"id":2,"parentId":0,"menuName":"1-2"},{"id":3,"parentId":0,"menuName":"1-3"}],
                    data: resultData.data,
                    icon_key: 'menuName',
                    primary_key: 'id',
                    parent_key: 'parentId',
                    // is_checkbox: true,
                    // checked: {
                    //     key: 'id',
                    //     data: [0,1,4,10,11,5,2,6,7,3,8,9],
                    // },
                    end: function(e){
                        form.render();
                    },
                    cols: [
                        {
                            key: 'menuName',
                            title: '菜单名称',
                            width: '100px',
                            template: function(item){
                                if(item.level == 0){
                                    return '<span style="color:red;">'+item.menuName+'</span>';
                                }else if(item.level == 1){
                                    return '<span style="color:green;">'+item.menuName+'</span>';
                                }else if(item.level == 2){
                                    return '<span style="color:#aaa;">'+item.menuName+'</span>';
                                }
                            }
                        },
                        {
                            key: 'menuKey',
                            title: '菜单关键字',
                            width: '100px',
                            align: 'left',
                        },
                        {
                            key: 'levelSelf',
                            title: '等级',
                            width: '50px',
                            align: 'center',
                        },
                        {
                            key: 'href',
                            title: '链接',
                            width: '100px',
                            align: 'left',
                        },
                        {
                            key: 'target',
                            title: '打开方式',
                            width: '70px',
                            align: 'center',
                        },
                        {
                            key: 'orderId',
                            title: '排序号',
                            width: '50px',
                            align: 'center',
                        },
                        {
                            key: 'comment',
                            title: '说明',
                            width: '250px',
                            align: 'center',
                        },
                        {
                            title: '操作',
                            align: 'center',
                            template: function(item){
                                var renderHtml = '';
                                if (menuManager_page.data.permissionEdit) {
                                    renderHtml = renderHtml + '<a class="layui-btn layui-btn-primary layui-btn-xs editBtn" data-id='+item.id+'>编辑</a>';
                                }
                                if (menuManager_page.data.permissionDelete) {
                                    renderHtml = renderHtml + '<a class="layui-btn layui-btn-primary layui-btn-xs deleteBtn" data-id='+item.id+' data-levelSelf='+item.levelSelf+'>删除</a>';
                                }
                                return renderHtml;
                            }
                        }
                    ]
                });

                $(".editBtn").click(function () {
                    menuManager_page.interface.openFormPage($(this).attr("data-id"));
                });
                $(".deleteBtn").click(function () {
                    var id = $(this).attr("data-id");
                    var levelSelf = $(this).attr("data-levelSelf");
                    AppComm.layer.confirm('确定要进行删除吗？',function () {
                        menuManager_page.action.deleteMenu(id,levelSelf);
                    });
                });
            })

            $("#addBtn").click(function () {
                menuManager_page.interface.openFormPage();
                menuManager_page.action.bindEvent();
                return false;
            });
        })
    },

    openFormPage:function(rowId) {
        AppComm.layer.open('/manager/form/menuManagerForm', null, function (layero, index) {
            var iframeWin = AppComm.utils.iframeWin(layero);
            if (rowId != undefined && '' != rowId.trim()) {
                iframeWin.menuManagerForm_page.data.rowId = rowId;
            }
            iframeWin.menuManagerForm_page.action.initBody();
        });
    },
};

menuManager_page.data = {
    'tableIns': undefined,
    'permissionEdit':false,
    'permissionDelete':false,
    'action':""
};

AppComm.initFrame(menuManager_page);
